<template>
  <el-dialog
    class="assign-role"
    title="分配角色"
    :visible="showRoleDialog"
    @close="handleDialogClose"
  >
    <el-checkbox-group v-model="selectRoles">
      <!-- 多选的选项 -->
      <el-checkbox v-for="role in roleList" :key="role.id" :label="role.id">{{
        role.name
      }}</el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-button type="primary" size="small" @click="saveRole">确定</el-button>
      <el-button size="small" @click="$emit('update:show-role-dialog', false)"
        >取消</el-button
      >
    </template>
  </el-dialog>
</template>

<script>
import { getRoleListApi } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRolesApi } from '@/api/employees'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      selectRoles: [],
      roleList: [],
      userId: ''
    }
  },
  created () {
    this.getRoleList()
  },
  methods: {
    handleDialogClose () {
      this.$emit('update:show-role-dialog', false)
      this.selectRoles = []
    },
    async getRoleList () {
      const data = await getRoleListApi({ page: 1, pagesize: 1000 })
      console.log(data)
      this.roleList = data.rows
    },
    async getUserDetail (id) {
      this.userId = id
      const data = await getUserDetailById(id)
      console.log(data)
      this.selectRoles = data.roleIds || []
    },
    async saveRole () {
      // data { id:当前用户id, roleIds:选中的角色id组成的数组 }
      await assignRolesApi({ id: this.userId, roleIds: this.selectRoles })
      this.$message.success('分配成功')
      this.$emit('update:show-role-dialog', false)
    }
  }
}
</script>

<style lang="scss" scoped>
.el-checkbox {
  margin-bottom: 20px;
}
</style>
